const koa = require('koa');
const koaLogger = require('koa-logger');
const koaBody = require('koa-body');
const koaRouter = require('koa-router');
const fs = require('mz/fs');

const app = new koa();

app.use(koaLogger());
app.use(koaBody({ multipart: true}));

const uploadRouter = new koaRouter();

uploadRouter.post('/', async (ctx, next) => {
  let file = ctx.request.files.file;
  ctx.response.body = { ret: 1000, file: (await fs.readFile(file.path)).toString() };
  await fs.unlink(file.path);
  return await next();
});

app.use(uploadRouter.routes());

const port = 8888;
app.listen(port);
console.log(`Server listen on ${port}`);

